<template>
  <div class="recommend" ref="recommend">
    <scroll class="recommend-content" :data="this.discList" ref="scroll">
      <div>
        <div class="slider-wrapper">
          <slider>
            <!--          这里增加v-if是保证从QQ音乐成功获取数据之后才能渲染a链接和img-->
            <div v-if="recommends.length" v-for="(item,index) in recommends" :key="index">
              <a :href="item.linkUrl">
                <img :src="item.picUrl" alt="图片丢失了">
              </a>
            </div>
          </slider>
        </div>
        <div class="recommend-list">
          <h1 class="list-title">热门歌单推荐</h1>
          <ul>
            <li v-for="(item,index) in discList" class="item" :key="index" @click="selectItem(item)">
              <!--            歌单图标-->
              <div class="icon">
                <!--                图标v-lazy懒加载-->
                <img v-lazy="item.imgurl" width="60" height="60" alt="图片丢失">
              </div>
              <!--            歌单外部描述-->
              <div class="text">
                <h2 class="name">{{item.creator.name}}</h2>
                <p class="desc">{{item.dissname}}</p>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <!--      正在加载的动画,当歌单因网络问题未加载出来时-->
      <div class="loading-content" v-show="!discList.length">
        <loading></loading>
      </div>
    </scroll>
    <router-view></router-view>
  </div>
</template>

<script type="text/ecmascript-6">
  import Slider from 'base/slider/slider'
  import Scroll from 'base/scroll/scroll'
  import Loading from 'base/loading/loading'
  import {getRecommend, getDiscList} from '@/api/recommend'
  import {mixinPlayList} from 'common/js/mixin'
  import {mapMutations} from 'vuex'

  export default {
    mixins: [mixinPlayList],
    data () {
      return {
        // 轮播图
        recommends: [],
        // 推荐歌单
        discList: []
      }
    },
    created () {
      this._getRecommend()
      this._getDiscList()
    },
    methods: {
      // 推荐歌单的点击事件，进行路由跳转
      selectItem (item) {
        // 跳转路由
        this.$router.push({
          path: `/recommend/${item.dissid}`
        })
        // vue改变装入被点击的推荐歌单
        this.setDisc(item)
      },
      // 解决mini播放器出现时，推荐歌单底部的bottom不足的问题
      handlePlayList (playlist) {
        const bottom = playlist.length > 0 ? '60px' : ''
        this.$refs.recommend.style.bottom = bottom
        this.$refs.scroll.refresh()
      },
      _getRecommend () {
        getRecommend().then((res) => {
          this.recommends = res.data.data.slider
        })
      },
      _getDiscList () {
        getDiscList().then((res) => {
          this.discList = res.data.list
        })
      },
      ...mapMutations({
        setDisc: 'SET_DISC'
      })
    },

    components: {
      Slider,
      Scroll,
      Loading
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable.styl"
  .recommend
    position: fixed
    width: 100%
    top: 88px
    bottom: 0

    .recommend-content
      height: 100%
      overflow: hidden

      .slider-wrapper
        position: relative
        width: 100%
        overflow: hidden

      .recommend-list
        .list-title
          height: 65px
          line-height: 65px
          text-align: center
          font-size: $font-size-medium
          color: $color-theme

        .item
          display: flex
          box-sizing: border-box
          align-items: center
          padding: 0 20px 20px 20px

          .icon
            // 只占据60px
            flex: 0 0 60px
            padding-right: 20px
            width: 60px

          .text
            display: flex
            // 占据60px之外的所有空间
            flex: 1
            // 垂直排列
            flex-direction: column
            // 水平方向居中对齐
            justify-content: center
            line-height: 20px
            overflow: hidden
            font-size: $font-size-medium

            .name
              margin-bottom: 10px
              color: $color-text

            .desc
              color: $color-text-d

      .loading-content
        position: absolute
        width: 100%
        top: 50%
        transform: translateY(-50%)
</style>
